package com.slife.serial.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.slife.serial.domain.TbEltic;
import com.slife.serial.domain.TbOrder;
import com.slife.serial.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.*;

@Service

public class SerialTaskService {

    public final static HashMap<String,String> map = new HashMap();

    static {
        map.put("进瞬时流量", "0604006300028062");
        map.put("出瞬时流量", "080400630002814C");
        map.put("正向流量累积整数08", "0804006B0004808C");
        map.put("正向流量累积整数06", "0604006B000481A2");
    }

//    public final static HashMap<String,String> map = new HashMap();
//    static {
//        map.put("瞬时流量", "080400630002814C");
//        map.put("瞬时流速", "080400650002614D");
//        map.put("正向流量累积整数", "0804006B0004808C");
//        map.put("正向流量累积小数", "0804006D0004608D");
//        map.put("反向流量累积整数", "0804006F0004C14D");
//        map.put("反向流量累积小数", "080400710004A14B");
//    }
//
//    public final static HashMap<String,String> map2 = new HashMap();
//    static {
//        map2.put("瞬时流量", "0604006300028062");
//        map2.put("瞬时流速", "0604006500026063");
//        map2.put("正向流量累积整数", "0604006B000481A2");
//        map2.put("正向流量累积小数", "0604006D000461A3");
//        map2.put("反向流量累积整数", "0604006F0004C063");
//        map2.put("反向流量累积小数", "060400710004A065");
//    }


    @Autowired
    private SerialPortHandler serialPortHandler;
    @Autowired
    private TbOrderService tbOrderService;
    @Autowired
    private TbElticService elticService;
    @Value("${server.slptime}")
    private Integer sleepTime;

    @Value("${server.siteName}")
    private String siteName;

    @Value("${server.siteId}")
    private String siteId;

    @Autowired
    private HttpTaskService httpTaskService;
    @Scheduled(fixedDelay = 3000 )
    public void task1() throws IOException {

        for (Map.Entry<String, String> entry : map.entrySet()) {
            serialPortHandler.sendMessage(entry.getValue());
            try {
                Thread.sleep(sleepTime);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

            System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());

        }
    }

    @Scheduled(cron = "30 59 */1 * * ?")
    public void task2(){

        List<TbEltic> tbEltics = elticService.selectAccumulatingByHour();
        TbOrder order = new TbOrder();
        order.setCreateTime(new Date());
        for (TbEltic eltic: tbEltics){
            order.setName(eltic.getName());
            order.setFlag(false);
            order.setCtime(eltic.getcTime());
            if (eltic.getName().startsWith("出水")){
            order.setOutValue(eltic.getCvalue());
            }else {
                order.setInValue(eltic.getCvalue());
            }
        }
        System.out.println("每小时统计任务执行----------- " + DateUtils.getTime());
        tbOrderService.save(order);
    }

    @Scheduled(cron = "20 * * * * ?")
    public void task3(){
        QueryWrapper<TbOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("flag",false);
        List<TbOrder> list = tbOrderService.list(queryWrapper);


        for (TbOrder item: list) {



        }


    }
//
//
//    @Scheduled(cron = "0 3 * * * ?")
//    public void task3(){
//
//
//        QueryWrapper<TbOrder> queryWrapper = new QueryWrapper<>();
//        queryWrapper.isNull("name");
//        queryWrapper.orderByDesc("create_time");
//        queryWrapper.last("limit 1");
//
//
//        TbOrder one = tbOrderService.getOne(queryWrapper);
//
//        if (one!=null){
//            JSONObject item1 = new JSONObject();
//            item1.put("tguid",siteId);
//            item1.put("transfername",siteName);
//            item1.put("reportSite",siteId);
//            item1.put("reportStation","01");
//            item1.put("did",one.getId());
//            item1.put("businessTime", one.getCreateTime());
//            item1.put("collectDate",one.getCreateTime());
//            item1.put("disposal",one.getOutValue()); //出水量
//            item1.put("sewageAmount", one.getInValue());  //进水量
//            String result = httpTaskService.sendPost(item1);
//            JSONObject jsonObject = JSONObject.parseObject(result);
//            if (jsonObject!=null && jsonObject.getInteger("status") == 200){
//                one.setName("1");
//                tbOrderService.updateById(one);
//            }
//        }
//}
//instantaneous_value  瞬时值
//        cumulative_value   累积值



}
